<?php

namespace App\Traits;

use Psr\Http\Message\ResponseInterface;

trait ResponseTrait
{
    /**
     * @param array|object|string $data
     * @param int $code
     * @return ResponseInterface
     */
    public function success(array|object|string $data = [], int $code = 0): ResponseInterface
    {
        $format = [
            'msg' => 'success',
            'code'    => $code,
            'data'    => &$data,
        ];

        return $this->response->json($format);
    }

    /**
     * @param string $msg
     * @param int $code
     * @param array|object|string $data
     * @return ResponseInterface
     */
    public function error(string $msg = '', int $code = 1, array|object|string $data = []): ResponseInterface
    {
        $format = [
            'code'=> $code,
            'msg' => $msg,
        ];
        if (!empty($data)) {
            $format['data'] = &$data;
        }
        return $this->response->json($format);
    }
}